package com.rem.se.my;

import java.util.Arrays;

/**
 * 选择排序
 * 不稳定 o(n^2)
 *
 * @author Rem
 * @date 2023-03-09
 */
public class SelectSort {

    public static void main(String[] args) {

        int[] data = new int[]{4, 1, 7, 5, 6, 9, 2, 8, 3};

        for (int i = 0; i < data.length - 1; i++) {
            //s:最小元素的索引
            int s = i;
            for (int j = s + 1; j < data.length; j++) {
                if (data[s] > data[j]) {
                    s = j;
                }
            }
            if (s != i) {
                swap(i, s, data);
            }
            System.out.println("sort:" + Arrays.toString(data));
        }
    }

    private static void swap(int i, int j, int[] data) {
        int t = data[i];
        data[i] = data[j];
        data[j] = t;
    }
}
